package dpss.server;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import dpss.commons.CustomLogger;
import dpss.commons.Player;

/**
 * 
 * @author Shewetaank Indora
 *
 */
public class TestData {
	
	private static CustomLogger logger = CustomLogger.getInstance();
	
	String TEST_DATA_PATH = "testdata.xml";
	
	public HashSet<Player> getTestPlayers(String iprange) throws ParserConfigurationException, SAXException, IOException{
		    
		    Set<Player> playerData = new HashSet<Player>();
			File xmlFile = new File(TEST_DATA_PATH);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(xmlFile);
			
			NodeList nodeList = doc.getElementsByTagName("player");
			
			for(int temp = 0; temp< nodeList.getLength(); temp++){
				Node _node = nodeList.item(temp);
				if(_node.getNodeType() == Node.ELEMENT_NODE){
					Element element = (Element) _node;
					if(element.getElementsByTagName("ipAddress").item(0).getTextContent().contains(iprange)){
						try {
							playerData.add(new Player(element.getElementsByTagName("userName").item(0).getTextContent(), 
									element.getElementsByTagName("password").item(0).getTextContent(), 
									Integer.parseInt(element.getElementsByTagName("age").item(0).getTextContent()), 
									element.getElementsByTagName("ipAddress").item(0).getTextContent(), 
									element.getElementsByTagName("firstName").item(0).getTextContent(), 
									element.getElementsByTagName("lastName").item(0).getTextContent(), 
									Boolean.parseBoolean(element.getElementsByTagName("status").item(0).getTextContent())));
						} catch (Exception e) {
							logger.log("ERROR - Could not add the admin account");
						}
					}
					
				}	
			}
			return (HashSet<Player>) playerData;
	}
}

